package com.jwx.mycar.controller;

import com.jwx.mycar.common.LabelEunm;
import com.jwx.mycar.entity.dto.DraftDto;
import com.jwx.mycar.entity.po.Draft;
import com.jwx.mycar.entity.vo.DraftVO;
import com.jwx.mycar.entity.vo.ResultVo;
import com.jwx.mycar.service.DraftService;
import com.jwx.mycar.utils.CopyProperties;
import com.jwx.mycar.utils.KeyUtil;
import com.jwx.mycar.utils.ResultVOUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName DraftController
 * @Description 帖子功能
 * @Author jwx
 * @Date 2019/11/22 16:26
 * @Version 1.0
 **/
@Api(tags = "帖子相关")
@RequestMapping("/draft")
@RestController
public class DraftController {
    @Autowired
    DraftService draftService;
    @Autowired
    ResultVOUtil resultVOUtil;
   /**
    *
    * @description:为识别结果增加帖子
    * @return:
    * @author: jwx
    * @time: 2019/11/27 20:06
    */
    @PostMapping(value = "/add")
    @ApiOperation(value = "增加帖子",notes ="标签要求传入int数组,数字对应的标签如下，暂时弄了五个标签(1,越野),(2,车轮)(3,雨刮器) (4,倒车镜), (5,车灯)")
    public ResultVo addDraft(@RequestBody DraftVO draftVO,
                             @RequestParam("labelsId") List<Integer> labelsId) throws ParseException//根据自己数据库的来写
    {
        DraftDto draftDto = CopyProperties.copy(draftVO,DraftDto.class);
        draftDto.setUserId(draftVO.getUserId());
        draftDto.setDraftId(KeyUtil.genUniqueKey());
        return draftService.addDraft(draftDto,labelsId);


    }
    /**
     *
     * @description: 根据帖子id获取帖子
     * @param null
     * @return:
     * @author: jwx
     * @time: 2019/12/6 21:12
     */

    @PostMapping(value = "/getDraftById")
    @ApiOperation(value = "根据帖子id获取帖子")
    @ApiImplicitParam(name = "draftId" ,value = "帖子id",required = true,dataType = "String",paramType = "query")
    public ResultVo getDraftById(@RequestParam("draftId") String draftId){
        Draft draft = draftService.getDraftById(draftId);
        return resultVOUtil.success(draft);
    }

    /**
     *
     * @description: 根据userId获取所有帖子
     * @param null
     * @return:
     * @author: jwx
     * @time: 2019/12/6 21:13
     */
    @ApiOperation(value="根据用户id获取所有帖子")
    @ApiImplicitParam(name = "userId", value = "用户id",required =true,dataType = "String",paramType = "query")
    @GetMapping("/getAllByUserId")
    public ResultVo getAllByUser(@RequestParam("userId")String userId){
        List<Draft> draftList = draftService.getAllByUserId(userId);
        return resultVOUtil.success(draftList);

    }


    /**
     *
     * @description: 获取所有帖子
     * @param null
     * @return:
     * @author: jwx
     * @time: 2019/12/6 21:12
     */


    @ApiOperation(value="获取所有帖子")
    @GetMapping("/getAll")
    public ResultVo getAll(){
        List<Draft> draftList = draftService.getAll();
        return resultVOUtil.success(draftList);

    }

    @ApiOperation(value = "根据帖子id获取所有标签" )
    @ApiImplicitParam(name = "draftId" ,value = "帖子id",required = true,dataType = "String",paramType = "query")
    @PostMapping("/getLabels")
    public ResultVo getLabels(@RequestParam("draftId") String draftId){
       List<Integer> labelIdList = draftService.getLabels(draftId);
       List<String>  labelList = new ArrayList<>();
       for(Integer labelId:labelIdList){
           LabelEunm[] values = LabelEunm.values();
           for(LabelEunm labelEunm:values){
               if(labelEunm.getLabelId()==labelId){
                   labelList.add(labelEunm.getLabelName());
               }
           }
       }
       return resultVOUtil.success(labelList);
    }


}
